<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis主-哨兵架构故障恢复机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e45e2 0%, #88d3ce 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .first-letter:first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.8;
            margin: 0.1em 0.2em 0 0;
            color: #6e45e2;
            font-weight: 700;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">Redis主-哨兵架构故障恢复机制</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8">探索高可用性架构中的自动故障转移与数据一致性保障</p>
            <div class="flex justify-center space-x-4">
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2 text-xl"></i>
                    <span>高可用性</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sync-alt mr-2 text-xl"></i>
                    <span>自动故障转移</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-database mr-2 text-xl"></i>
                    <span>数据一致性</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
        <!-- Concept Visualization -->
        <div class="bg-white rounded-xl shadow-lg p-8 mb-16">
            <h2 class="text-2xl font-bold mb-6 text-center text-gray-800">Redis主-哨兵架构故障恢复流程</h2>
            <div class="mermaid">
                graph TD
                A[主节点故障] --> B[哨兵检测到故障]
                B --> C[选举新的主节点]
                C --> D[通知所有从节点]
                D --> E[客户端重定向]
                E --> F[故障主节点恢复]
                F --> G[连接到哨兵获取状态]
                G --> H{是否仍然是主节点?}
                H --> |否| I[成为新主节点的从节点]
                I --> J[数据同步]
                H --> |是| K[继续作为主节点]
            </div>
        </div>

        <!-- Content Sections -->
        <div class="grid md:grid-cols-2 gap-8 mb-16">
            <!-- Section 1 -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-3 rounded-full mr-4">
                            <i class="fas fa-redo text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">主节点恢复后的初步处理</h3>
                    </div>
                    <p class="text-gray-600 first-letter">
                        当故障的主节点恢复后，它会尝试重新加入Redis集群。恢复的主节点会首先连接到哨兵，获取当前集群的状态信息，包括主节点和从节点的信息。
                    </p>
                    <div class="mt-4 bg-gray-50 p-4 rounded-lg">
                        <p class="text-sm text-gray-500 mb-2">关键步骤：</p>
                        <ul class="list-disc pl-5 text-gray-600 space-y-1">
                            <li>连接到哨兵获取集群状态</li>
                            <li>确认当前主节点信息</li>
                            <li>同步集群配置</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Section 2 -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-blue-100 p-3 rounded-full mr-4">
                            <i class="fas fa-exchange-alt text-blue-600 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">恢复主节点的状态</h3>
                    </div>
                    <p class="text-gray-600 first-letter">
                        恢复的主节点会根据哨兵提供的信息调整自身状态。如果发现不再是主节点，它会自动成为当前主节点的从节点，并开始数据同步过程。
                    </p>
                    <div class="mt-4 bg-gray-50 p-4 rounded-lg">
                        <p class="text-sm text-gray-500 mb-2">配置命令示例：</p>
                        <pre class="bg-gray-800 text-gray-100 p-3 rounded text-sm overflow-x-auto"><code>SLAVEOF &lt;new-master-ip&gt; &lt;new-master-port&gt;</code></pre>
                    </div>
                </div>
            </div>
        </div>

        <!-- Full Width Section -->
        <div class="bg-white rounded-xl shadow-md overflow-hidden mb-16 card-hover">
            <div class="p-8">
                <div class="flex items-center mb-6">
                    <div class="bg-green-100 p-3 rounded-full mr-4">
                        <i class="fas fa-check-circle text-green-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800">防止数据不一致</h3>
                </div>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <p class="text-gray-600 mb-4">
                            Redis在主-哨兵模式下有完善的数据一致性保障机制。恢复的主节点在成为从节点时会检查数据是否丢失，必要时进行全量同步。
                        </p>
                        <p class="text-gray-600">
                            哨兵会确认新主节点的数据完整性和一致性，确保恢复过程中不会引入数据不一致问题。
                        </p>
                    </div>
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h4 class="font-semibold text-gray-700 mb-3">一致性保障措施</h4>
                        <ul class="space-y-2 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>复制延迟检测</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>全量同步机制</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>数据校验检查</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- Configuration Section -->
        <div class="mb-16">
            <h2 class="text-2xl font-bold mb-6 text-center text-gray-800">优化配置与调优建议</h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="font-bold text-lg text-gray-800 mb-3">关键配置参数</h3>
                    <div class="space-y-4">
                        <div>
                            <h4 class="font-semibold text-gray-700 mb-1">min-slaves-to-write</h4>
                            <p class="text-gray-600 text-sm">指定主节点执行写操作时要求的最小从节点数量</p>
                        </div>
                        <div>
                            <h4 class="font-semibold text-gray-700 mb-1">min-slaves-max-lag</h4>
                            <p class="text-gray-600 text-sm">从节点最大延迟时间，超过此值则不考虑为有效从节点</p>
                        </div>
                        <div>
                            <h4 class="font-semibold text-gray-700 mb-1">sentinel monitor</h4>
                            <p class="text-gray-600 text-sm">哨兵监控配置，定义哨兵监控的主节点和法定人数</p>
                        </div>
                    </div>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="font-bold text-lg text-gray-800 mb-3">最佳实践</h3>
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span>部署至少3个哨兵节点以确保选举的可靠性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span>合理设置故障检测时间和故障转移超时</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span>监控复制延迟和哨兵健康状况</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span>定期测试故障转移流程</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col items-center">
                <h3 class="text-lg font-medium mb-2">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors duration-200">http://www.yuque.com/jtostring</a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>